using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;


public class CSdConnector_TCP : CsdSocket_TCP
{
    CProtocolMgr proMgr = null;

    public bool Connect(string ip, int port, CProtocolMgr pm)
    {
        try
        {
            if (pm == null)
            {
                return false;
            }
            proMgr = pm;
            IPAddress address = IPAddress.Parse(ip);
            IPEndPoint ipe = new IPEndPoint(address, port);
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            s.Connect(ipe);

            if (s.Connected)
            {
                s.Blocking = false;
                return true;
            }
        }
        catch (System.Exception ex)
        {
            UnityEngine.Debug.Log("connect err:"+ex.Message);
        }
        UnityEngine.Debug.Log("connect failed:" );
        return false;
    }
    public void Run()
    {
        try
        {

            int recvcount = 0;
            byte[] recvbuf = RecvAndReturn(ref recvcount);
            if (recvbuf != null && proMgr != null)
            {
                int movetohead = proMgr.Handle(recvbuf, recvcount, this);
                while (movetohead > 0 && recvcount >= 0)
                {
                    SetRecvIndex(movetohead);
                    recvcount -= movetohead;
                    movetohead = proMgr.Handle(recvbuf, recvcount, this);
                }
            }

        }
        catch (System.Exception)
        {

        }
    }
}


public class CMyProtocolMgrClient : CProtocolMgr
{
    public override void handle_PKT_GSCLI_TRIGGER_ACTION_ACK(PKT_GSCLI_TRIGGER_ACTION_ACK p, CsdSocket_TCP peer)
    {
        UnityEngine.Debug.Log("client recv msg from :" + peer.HandleID());
        //p.DebugPrint();
    }
    public override void handle_PKT_CLIGS_TRIGGER_DAMAGE_REQ(PKT_CLIGS_TRIGGER_DAMAGE_REQ p, CsdSocket_TCP peer)
    {
        UnityEngine.Debug.Log("client recv msg from :" + peer.HandleID());
        //p.DebugPrint();
    }
    public override void handle_PKT_CLIGS_SKILL_EVENT_REQ(PKT_CLIGS_SKILL_EVENT_REQ p, CsdSocket_TCP peer)
    {
        UnityEngine.Debug.Log("client recv msg from :" + peer.HandleID());
        //p.DebugPrint();
    }


}